package com.gemalto.Kryptik.Algo;


import java.util.Vector;
import com.gemalto.Kryptik.Algo.*;
import com.gemalto.Kryptik.Gui.*;
import com.gemalto.Kryptik.*;
import javax.swing.*;

public class AlgoManager {

	private DefaultListModel algoStore;
	private Vector nameStore;

	private int activeAlgo;
	private AlgoTab algoTab;
	private Indicator indicator;
	private String name;

	public AlgoManager(String name){
		algoStore=new DefaultListModel();
		nameStore=new Vector();
		this.name=name;
	}


	public void registerGui(AlgoTab ag){
		algoTab=ag;
	}


	public void registerIndicator(Indicator i){
		indicator = i;
	}

	
	public void addAlgo(AbstractAlgo a){
		int i = algoStore.size();
	 	algoStore.addElement(a);
		nameStore.add(i, a.getShortName());
	}


	public AbstractAlgo getAlgo(int id){
		return (AbstractAlgo)algoStore.get(id);
	}


	public AbstractAlgo getAlgo(String name){
		int i=nameStore.indexOf(name);
		return getAlgo(i);
	}


	public void setActiveAlgo(int id){
		try {
			getActiveAlgo().deactivate();
		} catch (AlgoException e){
			Messenger.error(e.getMessage());
                }
	 	activeAlgo=id;
		if ( algoTab!=null) algoTab.setCurrentAlgo(id);
		if ( indicator != null) indicator.setAlgoName(getAlgo(id).getShortName());
                try {
			getAlgo(id).activate();
		} catch (AlgoException e){
			Messenger.error(e.getMessage());
                }
	}


	public void setActiveAlgo(String name){
		int i=nameStore.indexOf(name);
		setActiveAlgo(i);
	}


	public AbstractAlgo getActiveAlgo(){
		return (AbstractAlgo)algoStore.get(activeAlgo);
	}





	public AlgoPanel getPanel(int id){
		return (AlgoPanel)getAlgo(id).getPanel();
	}		



	public DefaultListModel getStore(){
		return algoStore;
	}
		
	public String toString(){
		return name;
	}


}
// vim:tabstop=4:nowrap 
